<template>
    <view style="padding: 15px;">
        <u-form ref="validateFormRef" :model="validateForm.value" :label-width="200">
            <u-form-item label="选择推荐商品" prop="goodsId" required right-icon="arrow-right">
                <u-input v-model="validateForm.value.goodsTitle" type="textarea" height="40" :clearable="false" placeholder="请选择推荐商品" right-icon="arrow-right"
                         @click="navigateTo(`/pages/common/goods-recommend/goods-recommend-select/index`, null, validateForm.value)"/>
            </u-form-item>

            <u-form-item label="开始时间" prop="startTime" required>
                <u-input type="select" :select-open="validateForm.controls.startTime.isShow"
                         v-model="validateForm.value.startTime" placeholder="请选择开始时间"
                         @click="validateForm.controls.startTime.isShow = true; $forceUpdate();"/>
            </u-form-item>
            <u-form-item label="结束时间" prop="endTime" required>
                <u-input type="select" :select-open="validateForm.controls.endTime.isShow"
                         v-model="validateForm.value.endTime" placeholder="请选择结束时间"
                         @click="validateForm.controls.endTime.isShow = true; $forceUpdate();"/>
            </u-form-item>
            <u-form-item label="状态" prop="status" required>
                <u-input type="select" :select-open="validateForm.controls.status.isShow" v-model="validateForm.controls.status.textDesc"
                         placeholder="请选择状态" @click="validateForm.controls.status.isShow = true; $forceUpdate();"></u-input>
            </u-form-item>
            <u-form-item label="显示顺序" prop="displayOrder">
                <u-input v-model="validateForm.value.displayOrder" placeholder="请输入显示顺序（可选）"/>
            </u-form-item>
        </u-form>

        <u-button type="error" @click="submit">提交</u-button>

        <u-picker mode="time" :mask-close-able="false" v-model="validateForm.controls.startTime.isShow"
                  :default-time="validateForm.value.startTime" :params="pickerFormatDatetime"
                  @cancel="validateForm.controls.startTime.isShow = false; $forceUpdate();"
                  @confirm="UFormUtil.timeConfirm($event, validateForm.controls.startTime)"></u-picker>

        <u-picker mode="time" :mask-close-able="false" v-model="validateForm.controls.endTime.isShow"
                  :default-time="validateForm.value.endTime" :params="pickerFormatDatetime"
                  @cancel="validateForm.controls.endTime.isShow = false; $forceUpdate();"
                  @confirm="UFormUtil.timeConfirm($event, validateForm.controls.endTime)"></u-picker>

        <u-select mode="single-column" label-name="name" value-name="code" :mask-close-able="false"
                  v-model="validateForm.controls.status.isShow" :list="validateForm.controls.status.options"
                  :default-value="validateForm.controls.status.defaultValue"
                  @cancel="validateForm.controls.status.isShow = false; $forceUpdate();"
                  @confirm="UFormUtil.selectSingleColumnConfirm($event, validateForm.controls.status)"></u-select>
    </view>
</template>

<script>
    import {CommonUtil} from "../../../../dev/core/utils/common.util";
    import {UFormUtil} from "../../../../dev/core/utils/u-form.util";
    import {SysDictionaryService} from "../../../../dev/services/sys-dictionary.service";
    import {TbGoodsRecommendService} from "../../../../dev/services/tb-goods-recommend.service";
    import myImg from '@/components/my-img'

    export default {
        data() {
            return {
                validateForm: {
                    value: {
                        id: CommonUtil.randomUUID(),
                        fkId: '',
                        goodsId: '',
                        goodsTitle: '',
                        startTime: '',
                        endTime: '',
                        status: '1',
                        displayOrder: '',
                    },
                    rules: {
                        goodsId: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '必填项!', trigger: ['change', 'blur']}
                        ],
                        startTime: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.startTime && this.validateForm.value.endTime) {
                                        let startTime = new Date(this.validateForm.value.startTime).getTime();
                                        let endTime = new Date(this.validateForm.value.endTime).getTime();
                                        if (startTime > endTime) {
                                            callback(new Error('开始时间不能大于结束时间！'));
                                        } else {
                                            callback()
                                        }
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        endTime: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.startTime && this.validateForm.value.endTime) {
                                        let startTime = new Date(this.validateForm.value.startTime).getTime();
                                        let endTime = new Date(this.validateForm.value.endTime).getTime();
                                        if (startTime > endTime) {
                                            callback(new Error('开始时间不能大于结束时间！'));
                                        } else {
                                            callback()
                                        }
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        status: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '必填项!', trigger: ['change', 'blur']}
                        ],
                        displayOrder: [
                            {
                                pattern: /^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
                                message: '请输入大于0的正数!',
                                trigger: ['change', 'blur']
                            }
                        ],
                    }
                },
            }
        },
        components: {
            myImg,
        },
        watch: {
            'validateForm.value.goodsId': {
                deep: true, // 深度监听
                handler(newVal, oldVal) {
                    console.log("goodsId参数改变，即将刷新...", `新值：${newVal}`, `旧值：${oldVal}`);
                    UFormUtil.validation(this.$refs.validateFormRef, 'goodsId');
                }
            }
        },
        onReady() {
            const _self = this;
            this.validateForm.value.fkId = this.urlParams.fkId;
            new SysDictionaryService().optionList('status', null).then(result => {
                UFormUtil.setOptions(_self.validateForm.controls.status, result);
                _self.$forceUpdate();
            });
        },
        methods: {
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new TbGoodsRecommendService().saveTbGoodsRecommend(_self.validateForm.value).then(result => {
                                        CommonUtil.toastNavigateBack('保存成功');
                                        _self.objParams();
                                    });
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    };
</script>

<style lang="scss" scoped>
</style>
